package view;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;


import model.infocenter.ClientUserInformations;
import model.roomManagement.Implementation.RoomManageModel;
import model.roomManagement.Interface.IRoomManageModel;
import model.roomManagement.Interface.ViewObserver;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import controller.audio.AudioController;
import controller.audio.Interface.IAudioController;
import controller.roomManagement.Interface.IRoomManageController;
import controller.roomManagement.Implementation.RoomManageController;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;




public class RoomManageView implements ActionListener,ViewObserver{
	//房间管理界面的坐标
	private int x;
	private int y;
	
	RoomManageModel RMM; 
	IRoomManageController RMC;
	  
	ArrayList ipList = new ArrayList();
	ArrayList userList = new ArrayList(); 
	ArrayList roomList = new ArrayList(); 
	String userInfo = null;
	String roomInfo = null;
	 
	public RoomManageView(IRoomManageController roomManageController,IRoomManageModel rMM2,int x,int y){
		this.RMM = (RoomManageModel)rMM2;
	    this.RMC = (RoomManageController)roomManageController;
	    this.x = x;
	    this.y = y;
	    RMM.addViewObserver(this);	
	     
	    getUserInfo();
	}
		

	public boolean deletRoom(int roomId) {		
		return RMC.deletRoom(roomId);
	}

	public boolean quit(int roomId) {
		return RMC.quit(roomId);		
	}

	public boolean exit() {
		// TODO Auto-generated method stub
		return RMC.exit();
	}

	private void getUserInfo() {
		userInfo=((RoomManageModel)RMM).getUserInfo();
		
	}
	
	public void getRoomInfo(int roomId) {
		roomInfo=RMC.getRoomInfoByRId(roomId);		
	}
	
	public boolean enr(int roomId) {	
		return RMC.enter(roomId);
	}

	public boolean join(int roomId, String password) {	
		if(RMC.join(roomId, password)){
			return true;
		}
		return false;
	}
	
	public int creatRoom(String subject, String password) {
		int roomId = -1;
		roomId=RMC.creatRoom(subject, password);
		return roomId;	
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub	
	}

	//界面代码*********************************************************************************
	private String[] user;//用户信息
	private Display display;
	protected Shell shlGms;
	private Table table;
	private Cursor cs_hand;
	private Text text_joinID;
	private Text text_creatTheme;
	private Text text_creatPwd;
	
	private Label lbl_joinOK;
	private Label lbl_creatTheme;
	private Label lbl_creatPwd;
	
	private Button btn_join;
	private Button btn_creat;
	
	private Button btn_joinRe;
	private Button btn_creatRe;
	
	private Button btn_joinUp;
	private Button btn_creatUp;
	
	private Label label;
	
	private Label label_0;
	private Label label_1;
	private Label label_2;
	
	private Label lbl_joinID;
	private Label lbl_joinPwd;
	private Text text_joinPwd;
	private ToolItem toolItem_refresh;
	//private Tray tray;
	//private TrayItem trayItem;
	private Label lbl_creatOK;
	private Label lbl_creatInfo;
	
	private ToolItem toolItem_creat;
	
	private Label lbl_remind;
	
	private int openedRoomID;//已经打开的房间ID
	private boolean open = false;
	private ToolItem toolItem_pwd;
	
	private Text text_oldPwd;
	private Text text_newPwd;
	private Button btn_pwdUp;
	private Button btn_pwdEnter;
	private Button btn_pwdRe;
	
	private Label lbl_oldPwd;
	private Label lbl_newPwd;
	private Label lbl_pwdOK;
	private boolean msgOpen = false;//网络错误提示是否已经打开
	
	/**
	 * Launch the application.
	 * @param args
	 */
	
	/*public static void main(String[] args) {
		try {
			RoomManageView window = new RoomManageView();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	public RoomManageView() {
		// TODO Auto-generated constructor stub
	}
	
	
	/**
	 * Open the window.
	 * @wbp.parser.entryPoint
	 */
	
	public void open() {
		display = Display.getDefault();
		
		cs_hand = new Cursor(display,SWT.CURSOR_HAND);//鼠标手势
		
		user = userInfo.split("/");
		
		//tray = display.getSystemTray();
		createContents();
		shlGms.open();
		shlGms.layout();
		while (!shlGms.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlGms = new Shell(SWT.CLOSE | SWT.MIN);
		
		shlGms.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				System.exit(0);
			}
		});
		shlGms.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/GMS.png"));
		shlGms.setSize(280, 505);
		shlGms.setLocation(x, y);
		shlGms.setText("GMS");
		
		label_0 = new Label(shlGms, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_0.setBounds(0, 60, 275, 2);
		
		label_1 = new Label(shlGms, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setBounds(0, 141, 275, 2);
		label_1.setVisible(false);
		
		label_2 = new Label(shlGms, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setBounds(0, 92, 275, 2);
		label_2.setVisible(false);
		//加入房间组件********************************************************************
		text_joinID = new Text(shlGms, SWT.BORDER);
		text_joinID.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				char c = e.character;
				if(!(Character.isDigit(c) || e.keyCode==8 || e.keyCode==13)){
					e.doit = false;
				}
				if(e.keyCode == 13){
					text_joinPwd.forceFocus();
				}
			}
		});
		text_joinID.setBounds(83, 67, 181, 20);
		text_joinID.setVisible(false);
		
		text_joinPwd = new Text(shlGms, SWT.BORDER | SWT.PASSWORD);
		text_joinPwd.setBounds(83, 91, 181, 20);
		text_joinPwd.setVisible(false);
		//快捷响应
		text_joinPwd.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13){
					//文本框为空
					if(text_joinID.getText().equals("") || text_joinPwd.getText().equals("")){
						BackgroundThread bt = new BackgroundThread("帐号或密码不能为空！");
						bt.start();
					}
					else{
						TableItem[] items = table.getItems();
						boolean isJoin = false;
						for(int i=0;i < items.length;i++){
							if(text_joinID.getText().equals(items[i].getText())){						
								isJoin = true;
								break;
							}
						}
						if(isJoin){
							BackgroundThread bt = new BackgroundThread("您已加入房间" + text_joinID.getText());
							bt.start();
						}
						else{
							//System.out.println(RMC.getRoomInfoByRId(Integer.parseInt(text_joinID.getText())));
							if(RMC.getRoomInfoByRId(Integer.parseInt(text_joinID.getText())).startsWith("0")){							
								BackgroundThread bt = new BackgroundThread("该房间不存在");
								bt.start();
							}
							else{
								String[] roomInfo = RMC.getRoomInfoByRId(Integer.parseInt(text_joinID.getText())).split("/");						
								String[] roomUser = roomInfo[3].split(",");
								if(roomUser.length == 8){
									BackgroundThread bt = new BackgroundThread("房间人数已超过上限，不能加入");
									bt.start();
								}
								else{
									//帐号密码正确，加入成功
									if(join(Integer.parseInt(text_joinID.getText()), text_joinPwd.getText())){ 
										text_joinID.setVisible(false);
										text_joinPwd.setVisible(false);
										
										lbl_joinID.setVisible(false);
										lbl_joinPwd.setVisible(false);
										
										btn_join.setVisible(false);
										
										btn_joinRe.setVisible(true);
										lbl_joinOK.setVisible(true);
										
										lbl_joinOK.setText("房间" + text_joinID.getText() + "加入成功！");
										refresh();//刷新列表
									}
									//帐号或密码错误，重新加入
									else{
										BackgroundThread bt = new BackgroundThread("房间帐号或密码不正确！");
										bt.start();
									}
								}
							}
						}
					}
				}
			}
		});
		btn_join = new Button(shlGms, SWT.NONE);
		btn_join.setToolTipText("点击加入房间");
		btn_join.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//文本框为空
				if(text_joinID.getText().equals("") || text_joinPwd.getText().equals("")){
					BackgroundThread bt = new BackgroundThread("帐号或密码不能为空！");
					bt.start();
				}
				else{
					TableItem[] items = table.getItems();
					boolean isJoin = false;
					for(int i=0;i < items.length;i++){
						if(text_joinID.getText().equals(items[i].getText())){						
							isJoin = true;
							break;
						}
					}
					if(isJoin){
						BackgroundThread bt = new BackgroundThread("您已加入房间" + text_joinID.getText());
						bt.start();
					}
					else{
						//System.out.println(RMC.getRoomInfoByRId(Integer.parseInt(text_joinID.getText())));
						if(RMC.getRoomInfoByRId(Integer.parseInt(text_joinID.getText())).startsWith("0")){							
							BackgroundThread bt = new BackgroundThread("该房间不存在");
							bt.start();
						}
						else{
							String[] roomInfo = RMC.getRoomInfoByRId(Integer.parseInt(text_joinID.getText())).split("/");						
							String[] roomUser = roomInfo[3].split(",");
							if(roomUser.length == 8){
								BackgroundThread bt = new BackgroundThread("房间人数已超过上限，不能加入");
								bt.start();
							}
							else{
								//帐号密码正确，加入成功
								if(join(Integer.parseInt(text_joinID.getText()), text_joinPwd.getText())){ 
									text_joinID.setVisible(false);
									text_joinPwd.setVisible(false);
									
									lbl_joinID.setVisible(false);
									lbl_joinPwd.setVisible(false);
									
									btn_join.setVisible(false);
									
									btn_joinRe.setVisible(true);
									lbl_joinOK.setVisible(true);
									
									lbl_joinOK.setText("房间" + text_joinID.getText() + "加入成功！");
									refresh();//刷新列表
								}
								//帐号或密码错误，重新加入
								else{
									BackgroundThread bt = new BackgroundThread("房间帐号或密码不正确！");
									bt.start();
								}
							}
						}
					}
				}
			}
		});
		btn_join.setBounds(53, 114, 60, 24);
		btn_join.setText("加入");
		btn_join.setVisible(false);
		
		btn_joinUp = new Button(shlGms, SWT.NONE);
		btn_joinUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btn_joinUp.setVisible(false);
				btn_joinRe.setVisible(false);
				btn_join.setVisible(false);
				
				text_joinID.setVisible(false);
				text_joinPwd.setVisible(false);
				
				lbl_joinID.setVisible(false);
				lbl_joinPwd.setVisible(false);
				lbl_joinOK.setVisible(false);
				
				label_1.setVisible(false);
				
				label.setBounds(10, 67,254, 20);
				table.setBounds(0, 90, 274, 388);
			}
		});
		btn_joinUp.setBounds(166, 114, 60, 24);
		btn_joinUp.setText("收起");
		btn_joinUp.setVisible(false);
		
		btn_joinRe = new Button(shlGms, SWT.NONE);
		btn_joinRe.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btn_joinRe.setVisible(false);
				lbl_joinOK.setVisible(false);
				
				text_joinID.setVisible(true);
				text_joinPwd.setVisible(true);
				
				lbl_joinID.setVisible(true);
				lbl_joinPwd.setVisible(true);
				
				btn_join.setVisible(true);
			}
		});
		btn_joinRe.setBounds(53, 114, 60, 24);
		btn_joinRe.setText("返回");
		btn_joinRe.setVisible(false);
		//*************************************************************************************
		
		//创建房间组件*************************************************************************************
		text_creatTheme = new Text(shlGms, SWT.BORDER);
		text_creatTheme.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13){
					text_creatPwd.forceFocus();
				}
			}
		});
		text_creatTheme.setBounds(83, 67, 181, 20);
		text_creatTheme.setVisible(false);
		
		text_creatPwd = new Text(shlGms, SWT.BORDER);
		//快捷响应
		text_creatPwd.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13){
					//文本框为空
					if(text_creatTheme.getText().equals("") || text_creatPwd.getText().equals("")){
						BackgroundThread bt = new BackgroundThread("帐号或密码不能为空！");
						bt.start();
					}
					else{
						text_creatTheme.setVisible(false);
						text_creatPwd.setVisible(false);
						
						lbl_creatTheme.setVisible(false);
						lbl_creatPwd.setVisible(false);
						
						btn_creat.setVisible(false);
						
						btn_creatRe.setVisible(true);
						
						lbl_creatOK.setText("房间 " + text_creatTheme.getText() + " 创建成功！");
						lbl_creatInfo.setText("帐号为：" + creatRoom(text_creatTheme.getText(), text_creatPwd.getText())
								+ "，密码为：" + text_creatPwd.getText());
						lbl_creatOK.setVisible(true);
						lbl_creatInfo.setVisible(true);
						refresh();//刷新列表
					}
				}
			}
		});
		text_creatPwd.setBounds(83, 91, 181, 20);
		text_creatPwd.setVisible(false);
		
		btn_creat = new Button(shlGms, SWT.NONE);
		btn_creat.setToolTipText("点击创建房间");
		btn_creat.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//文本框为空
				if(text_creatTheme.getText().equals("") || text_creatPwd.getText().equals("")){
					BackgroundThread bt = new BackgroundThread("帐号或密码不能为空！");
					bt.start();
				}
				else{
					text_creatTheme.setVisible(false);
					text_creatPwd.setVisible(false);
					
					lbl_creatTheme.setVisible(false);
					lbl_creatPwd.setVisible(false);
					
					btn_creat.setVisible(false);
					
					btn_creatRe.setVisible(true);
					
					lbl_creatOK.setText("房间 " + text_creatTheme.getText() + " 创建成功！");
					lbl_creatInfo.setText("帐号为：" + creatRoom(text_creatTheme.getText(), text_creatPwd.getText())
							+ "，密码为：" + text_creatPwd.getText());
					lbl_creatOK.setVisible(true);
					lbl_creatInfo.setVisible(true);
					refresh();//刷新列表
				}
			}
		});
		btn_creat.setBounds(53, 114, 60, 24);
		btn_creat.setText("创建");
		btn_creat.setVisible(false);
		
		btn_creatUp = new Button(shlGms, SWT.NONE);
		btn_creatUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btn_creatUp.setVisible(false);
				btn_creatRe.setVisible(false);
				btn_creat.setVisible(false);
				
				text_creatTheme.setVisible(false);
				text_creatPwd.setVisible(false);
				
				lbl_creatTheme.setVisible(false);
				lbl_creatPwd.setVisible(false);
				lbl_creatOK.setVisible(false);
				lbl_creatInfo.setVisible(false);
				
				label_1.setVisible(false);
				
				label.setBounds(10, 67,254, 20);
				table.setBounds(0, 90, 274, 388);
			}
		});
		btn_creatUp.setBounds(166, 114, 60, 24);
		btn_creatUp.setText("收起");
		btn_creatUp.setVisible(false);
		
		btn_creatRe = new Button(shlGms, SWT.NONE);
		btn_creatRe.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btn_creatRe.setVisible(false);
				lbl_creatOK.setVisible(false);
				lbl_creatInfo.setVisible(false);
				
				text_creatTheme.setVisible(true);
				text_creatPwd.setVisible(true);
				
				lbl_creatTheme.setVisible(true);
				lbl_creatPwd.setVisible(true);
				
				btn_creat.setVisible(true);
			}
		});
		btn_creatRe.setBounds(53, 114, 60, 24);
		btn_creatRe.setText("返回");
		btn_creatRe.setVisible(false);
		
		text_oldPwd = new Text(shlGms, SWT.BORDER | SWT.PASSWORD);
		text_oldPwd.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13){
					text_newPwd.forceFocus();
				}
			}
		});
		text_oldPwd.setBounds(83, 67, 181, 20);
		text_oldPwd.setVisible(false);
		
		text_newPwd = new Text(shlGms, SWT.BORDER | SWT.PASSWORD);
		text_newPwd.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13){

					if(text_newPwd.equals("") ||  text_oldPwd.equals("")){
						BackgroundThread bt = new BackgroundThread("密码不能为空");
						bt.start();
					}
					else{
						String oldPassword = text_oldPwd.getText();
						String newPassword = text_newPwd.getText();
						if(RMC.changePassWord(oldPassword, newPassword)){
							text_oldPwd.setVisible(false);
							text_newPwd.setVisible(false);
							btn_pwdEnter.setVisible(false);
							lbl_oldPwd.setVisible(false);
							lbl_newPwd.setVisible(false);
							
							btn_pwdRe.setVisible(true);
							lbl_pwdOK.setVisible(true);
						}
						else{
							BackgroundThread bt = new BackgroundThread("原密码不正确");
							bt.start();
						}
					}
				
				}
			}
		});
		text_newPwd.setBounds(83, 91, 181, 20);
		text_newPwd.setVisible(false);
		
		btn_pwdEnter = new Button(shlGms, SWT.NONE);
		btn_pwdEnter.setToolTipText("点击修改登录密码");
		btn_pwdEnter.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(text_newPwd.getText().equals("") || text_oldPwd.getText().equals("")){
					BackgroundThread bt = new BackgroundThread("密码不能为空");
					bt.start();
				}
				else{
					String oldPassword = text_oldPwd.getText();
					String newPassword = text_newPwd.getText();
					if(RMC.changePassWord(oldPassword, newPassword)){
						text_oldPwd.setVisible(false);
						text_newPwd.setVisible(false);
						btn_pwdEnter.setVisible(false);
						lbl_oldPwd.setVisible(false);
						lbl_newPwd.setVisible(false);
						
						btn_pwdRe.setVisible(true);
						lbl_pwdOK.setVisible(true);
					}
					else{
						BackgroundThread bt = new BackgroundThread("原密码不正确");
						bt.start();
					}
				}
			}
		});
		btn_pwdEnter.setBounds(53, 114, 60, 24);
		btn_pwdEnter.setText("确认");
		btn_pwdEnter.setVisible(false);
		
		btn_pwdUp = new Button(shlGms, SWT.NONE);
		btn_pwdUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//隐藏修改密码的组件************************
				text_oldPwd.setVisible(false);
				text_newPwd.setVisible(false);
				btn_pwdUp.setVisible(false);
				btn_pwdEnter.setVisible(false);
				btn_pwdRe.setVisible(false);
				
				lbl_oldPwd.setVisible(false);
				lbl_newPwd.setVisible(false);
				lbl_pwdOK.setVisible(false);
				//隐藏修改密码的组件***结束*********************
				label_1.setVisible(false);
				
				label.setBounds(10, 67,254, 20);
				table.setBounds(0, 90, 274, 388);
			}
		});
		btn_pwdUp.setBounds(166, 114, 60, 24);
		btn_pwdUp.setText("收起");
		btn_pwdUp.setVisible(false);
		
		btn_pwdRe = new Button(shlGms, SWT.NONE);
		btn_pwdRe.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lbl_pwdOK.setVisible(false);
				btn_pwdRe.setVisible(false);
				
				lbl_oldPwd.setVisible(true);
				lbl_newPwd.setVisible(true);
				
				text_newPwd.setText("");
				text_oldPwd.setText("");
				
				text_newPwd.setVisible(true);
				text_oldPwd.setVisible(true);
				
				btn_pwdEnter.setVisible(true);
			}
		});
		btn_pwdRe.setBounds(53, 114, 60, 24);
		btn_pwdRe.setText("返回");
		btn_pwdRe.setVisible(false);
		
		//创建房间组建***结束********************************************************************************
		
		final Label lbl_user = new Label(shlGms, SWT.NONE);		
		lbl_user.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				lbl_user.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
			}
			@Override
			public void mouseExit(MouseEvent e) {
				lbl_user.setBackground(null);
			}
		});
		lbl_user.setAlignment(SWT.CENTER);
		lbl_user.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/software_48.jpg"));
		lbl_user.setBounds(10, 4, 50, 50);
		lbl_user.setCursor(cs_hand);
		
		//*************************************************************
		
		
		
		table = new Table(shlGms, SWT.BORDER | SWT.FULL_SELECTION);		
		table.setLinesVisible(true);
		table.setBounds(0, 90, 274, 388);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(250);
		
	    for(int id:(ArrayList<Integer>)RMM.getRoomList()){
	    	TableItem ti =new TableItem(table, SWT.NONE); //
	    	ti.setText(id + "");
	    	ti.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
	    	String[] roomInfo = RMC.getRoomInfoByRId(id).split("/");
	    	if(roomInfo[1].equals(user[1])){
	    		ti.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群主群头像_35.png"));
	    	}
	    	else{
	    		ti.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群头像_35.png"));
	    	}
	    	
	    }
		
		final Menu menu = new Menu(table);
		
		MenuItem menuItem_detail = new MenuItem(menu, SWT.NONE);
		menuItem_detail.setText("\u4F1A\u8BAE\u5BA4\u8BE6\u60C5");
		menuItem_detail.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int roomID = Integer.parseInt(table.getItem(table.getSelectionIndex()).getText());
				Detail dt = new Detail(RMC,roomID);
				dt.open();
			}
		});
		
		final MenuItem menuItem_quit = new MenuItem(menu, SWT.NONE);
		menuItem_quit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int roomID = Integer.parseInt(table.getItem(table.getSelectionIndex()).getText());
				String[] roomInfo = RMC.getRoomInfoByRId(roomID).split("/");
				if(roomID == openedRoomID && RMM.isOpen()){
					BackgroundThread bt = new BackgroundThread("请关闭房间后再删除/退出");
					bt.start();
				}
				else{
					if(user[1].equals(roomInfo[1])){
						RMC.deletRoom(roomID);
						BackgroundThread bt = new BackgroundThread("您已成功删除房间" + roomID);
						bt.start();
					}
					else{
						RMC.quit(roomID);
						BackgroundThread bt = new BackgroundThread("您已成功退出房间" + roomID);
						bt.start();
					}
					table.remove(table.getSelectionIndices());
				}
			}
		});
    	//表格响应*****************************************************************************
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				Point point = new Point(e.x,e.y);
				//空白地方右击不弹出菜单
				if((e.button == 3) && (table.getItem(point) != null)){	
					table.setMenu(menu);
					int roomID = Integer.parseInt(table.getItem(table.getSelectionIndex()).getText());
					String[] roomInfo = RMC.getRoomInfoByRId(roomID).split("/");
					
					if(user[1].equals(roomInfo[1])){
						menuItem_quit.setText("删除房间");
					}
					else{
						menuItem_quit.setText("退出房间");
					}
				}
				else{
					table.setMenu(null);
				}
			}
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				Point point = new Point(e.x,e.y);
				if(table.getItem(point) != null){
					if(RMM.isOpen() == false){//RMM负责
						int roomID = Integer.parseInt(table.getItem(point).getText());
						ClientUserInformations.getInstance().setCurRoomId(roomID);						
						GMS gms = new GMS(RMM,roomID);//需要传值
						openedRoomID = roomID;
						RMM.setOpen(true);
						open = true;//房间管理界面的open状态
						refresh();//刷新列表
						RMC.enter(roomID);					
						gms.open();
					}
					else{
						BackgroundThread bt = new BackgroundThread("请关闭已进入的房间!");
						bt.start();
					}
				}
			}
		});
		//表格响应***结束**************************************************************************
		
		Composite composite = new Composite(shlGms, SWT.NONE);
		composite.setBounds(0, 0, 275, 480);
		composite.setBackgroundImage(SWTResourceManager.getImage(RoomManageView.class, "/img/背景_275_200.jpg"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);//透明
		
		lbl_joinID = new Label(composite, SWT.NONE);
		lbl_joinID.setBounds(10, 67, 61, 20);
		lbl_joinID.setText("房间帐号：");
		lbl_joinID.setVisible(false);
		
		lbl_joinPwd = new Label(composite, SWT.NONE);
		lbl_joinPwd.setBounds(10, 91, 61, 20);
		lbl_joinPwd.setText("房间密码：");
		lbl_joinPwd.setVisible(false);
		
		lbl_joinOK = new Label(composite, SWT.NONE);
		lbl_joinOK.setAlignment(SWT.CENTER);
		lbl_joinOK.setBounds(0, 78, 280, 20);
		lbl_joinOK.setText("加入成功！");
		lbl_joinOK.setVisible(false);
		
		lbl_creatOK = new Label(composite, SWT.NONE);
		lbl_creatOK.setAlignment(SWT.CENTER);
		lbl_creatOK.setBounds(0, 67, 280, 20);
		lbl_creatOK.setVisible(false);
		
		lbl_creatInfo = new Label(composite, SWT.NONE);
		lbl_creatInfo.setAlignment(SWT.CENTER);
		lbl_creatInfo.setBounds(0, 91, 280, 20);
		lbl_creatInfo.setVisible(false);
		
		lbl_creatTheme = new Label(composite, SWT.NONE);
		lbl_creatTheme.setLocation(10, 67);
		lbl_creatTheme.setSize(61, 20);
		lbl_creatTheme.setText("房间主题：");
		lbl_creatTheme.setVisible(false);
		
		
		lbl_creatPwd = new Label(composite, SWT.NONE);
		lbl_creatPwd.setLocation(10, 91);
		lbl_creatPwd.setSize(61, 20);
		lbl_creatPwd.setText("房间密码：");
		lbl_creatPwd.setVisible(false);
		
		lbl_oldPwd = new Label(composite, SWT.NONE);
		lbl_oldPwd.setAlignment(SWT.CENTER);
		lbl_oldPwd.setBounds(10, 67, 61, 20);
		lbl_oldPwd.setText("旧密码：");
		lbl_oldPwd.setVisible(false);
		
		lbl_newPwd = new Label(composite, SWT.NONE);
		lbl_newPwd.setAlignment(SWT.CENTER);
		lbl_newPwd.setBounds(10, 91, 61, 20);
		lbl_newPwd.setText("新密码：");
		lbl_newPwd.setVisible(false);
		
		lbl_pwdOK = new Label(composite, SWT.NONE);
		lbl_pwdOK.setAlignment(SWT.CENTER);
		lbl_pwdOK.setBounds(0, 79, 280, 20);
		lbl_pwdOK.setText("密码修改成功！");
		lbl_pwdOK.setVisible(false);
		
		label = new Label(composite, SWT.NONE);
		label.setText("已加入的会议房间：");
		label.setLocation(10, 67);
		label.setSize(254, 20);
		
		lbl_remind = new Label(composite, SWT.NONE);
		lbl_remind.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lbl_remind.setAlignment(SWT.CENTER);
		lbl_remind.setBounds(10, 67, 254, 20);
		lbl_remind.setVisible(false);
		
		ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);
		toolBar.setBounds(83, 28, 181, 26);
		
		toolItem_creat = new ToolItem(toolBar, SWT.NONE);		
		toolItem_creat.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lbl_remind.setVisible(false);
				label_2.setVisible(false);
				if(btn_creatUp.isVisible()){
					//隐藏创建房间的组件****************************
					btn_creat.setVisible(false);
					btn_creatUp.setVisible(false);
					btn_creatRe.setVisible(false);
					
					lbl_creatTheme.setVisible(false);
					lbl_creatPwd.setVisible(false);
					lbl_creatOK.setVisible(false);
					lbl_creatInfo.setVisible(false);
					
					text_creatTheme.setVisible(false);
					text_creatPwd.setVisible(false);
					//隐藏创建房间的组件***结束*************************
					label_1.setVisible(false);
					
					label.setBounds(10, 67,254, 20);
					table.setBounds(0, 90, 274, 388);
				}
				else{
					//隐藏加入房间的组件********************************
					text_joinPwd.setVisible(false);
					text_joinID.setVisible(false);
					
					btn_join.setVisible(false);
					btn_joinRe.setVisible(false);
					btn_joinUp.setVisible(false);
					
					lbl_joinOK.setVisible(false);
					lbl_joinID.setVisible(false);
					lbl_joinPwd.setVisible(false);
					//********************************************
					//隐藏修改密码的组件************************
					text_oldPwd.setVisible(false);
					text_newPwd.setVisible(false);
					btn_pwdUp.setVisible(false);
					btn_pwdEnter.setVisible(false);
					btn_pwdRe.setVisible(false);
					
					lbl_oldPwd.setVisible(false);
					lbl_newPwd.setVisible(false);
					lbl_pwdOK.setVisible(false);
					//隐藏修改密码的组件***结束*********************
					btn_creat.setVisible(true);
					btn_creatUp.setVisible(true);
					
					lbl_creatTheme.setVisible(true);
					lbl_creatPwd.setVisible(true);
					
					text_creatTheme.setText("");
					text_creatTheme.setVisible(true);
					text_creatPwd.setText("");
					text_creatPwd.setVisible(true);
					
					
					btn_creatRe.setVisible(false);
					lbl_creatOK.setVisible(false);
					lbl_creatInfo.setVisible(false);
					
					label_1.setVisible(true);
					
					label.setBounds(10, 147,254, 20);
					table.setBounds(0, 170, 274, 308);
				}
			}
		});
		toolItem_creat.setToolTipText("创建房间");
		toolItem_creat.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/\u65B0\u623F\u95F4_20.png"));
		
		ToolItem toolItem_join = new ToolItem(toolBar, SWT.NONE);
		toolItem_join.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lbl_remind.setVisible(false);
				label_2.setVisible(false);
				if(btn_joinUp.isVisible()){
					//隐藏加入房间的组建******************
					text_joinID.setVisible(false);
					text_joinPwd.setVisible(false);
					
					btn_join.setVisible(false);
					btn_joinRe.setVisible(false);
					btn_joinUp.setVisible(false);
					
					lbl_joinOK.setVisible(false);
					lbl_joinID.setVisible(false);
					lbl_joinPwd.setVisible(false);
					//隐藏加入房间的组建***结束***************
					
					label_1.setVisible(false);
					
					label.setBounds(10, 67,254, 20);
					table.setBounds(0, 90, 274, 388);
				}
				else{
					//隐藏创建房间的组建*******************
					btn_creat.setVisible(false);
					btn_creatRe.setVisible(false);
					btn_creatUp.setVisible(false);
					
					text_creatTheme.setVisible(false);
					text_creatPwd.setVisible(false);
					
					lbl_creatTheme.setVisible(false);
					lbl_creatPwd.setVisible(false);
					lbl_creatOK.setVisible(false);
					lbl_creatInfo.setVisible(false);
					//*********************************
					//隐藏修改密码的组件************************
					text_oldPwd.setVisible(false);
					text_newPwd.setVisible(false);
					btn_pwdUp.setVisible(false);
					btn_pwdEnter.setVisible(false);
					btn_pwdRe.setVisible(false);
					
					lbl_oldPwd.setVisible(false);
					lbl_newPwd.setVisible(false);
					lbl_pwdOK.setVisible(false);
					//隐藏修改密码的组件***结束*********************
					
					text_joinID.setText("");
					text_joinID.setVisible(true);
					text_joinPwd.setText("");
					text_joinPwd.setVisible(true);
					
					btn_join.setVisible(true);
					btn_joinUp.setVisible(true);
					btn_joinRe.setVisible(false);
					
					lbl_joinOK.setVisible(false);
					lbl_joinID.setVisible(true);
					lbl_joinPwd.setVisible(true);
					
					label_1.setVisible(true);
					
					label.setBounds(10, 147,254, 20);
					table.setBounds(0, 170, 274, 308);
				}
			}
		});
		toolItem_join.setToolTipText("加入房间");
		toolItem_join.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/\u52A0\u5165\u623F\u95F4_20.png"));
		
		toolItem_pwd = new ToolItem(toolBar, SWT.NONE);
		toolItem_pwd.setToolTipText("修改登录密码");
		toolItem_pwd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lbl_remind.setVisible(false);
				label_2.setVisible(false);
				if(btn_pwdUp.isVisible()){
					//隐藏修改密码的组件************************
					text_oldPwd.setVisible(false);
					text_newPwd.setVisible(false);
					btn_pwdUp.setVisible(false);
					btn_pwdEnter.setVisible(false);
					btn_pwdRe.setVisible(false);
					
					lbl_oldPwd.setVisible(false);
					lbl_newPwd.setVisible(false);
					lbl_pwdOK.setVisible(false);
					//隐藏修改密码的组件***结束*********************
					label_1.setVisible(false);
					
					label.setBounds(10, 67,254, 20);
					table.setBounds(0, 90, 274, 388);
				}
				else{
					//隐藏创建房间的组建*******************
					btn_creat.setVisible(false);
					btn_creatRe.setVisible(false);
					btn_creatUp.setVisible(false);
					
					text_creatTheme.setVisible(false);
					text_creatPwd.setVisible(false);
					
					lbl_creatTheme.setVisible(false);
					lbl_creatPwd.setVisible(false);
					lbl_creatOK.setVisible(false);
					lbl_creatInfo.setVisible(false);
					//*********************************
					
					//隐藏加入房间的组件********************************
					text_joinPwd.setVisible(false);
					text_joinID.setVisible(false);
					
					btn_join.setVisible(false);
					btn_joinRe.setVisible(false);
					btn_joinUp.setVisible(false);
					
					lbl_joinOK.setVisible(false);
					lbl_joinID.setVisible(false);
					lbl_joinPwd.setVisible(false);
					//********************************************
					
					text_newPwd.setText("");
					text_oldPwd.setText("");
					
					lbl_oldPwd.setVisible(true);
					lbl_newPwd.setVisible(true);
					
					text_oldPwd.setVisible(true);
					text_newPwd.setVisible(true);
					
					btn_pwdEnter.setVisible(true);
					btn_pwdUp.setVisible(true);
					
					label_1.setVisible(true);
					
					label.setBounds(10, 147,254, 20);
					table.setBounds(0, 170, 274, 308);
				}
			}
		});
		toolItem_pwd.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/password_20.png"));
		
		toolItem_refresh = new ToolItem(toolBar, SWT.NONE);
		toolItem_refresh.setToolTipText("刷新房间列表");
		toolItem_refresh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				refresh();//刷新列表
				BackgroundThread bt = new BackgroundThread("刷新成功！");
				bt.start();
			}
		});
		toolItem_refresh.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/refresh_new_22.png"));
		
		final Label lbl_name = new Label(composite, SWT.NONE);	
		lbl_name.setText(user[0]);
		lbl_name.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		lbl_name.setBounds(83, 4, 181, 20);
		
		//系统托盘***************************************************************************

		/*final Menu menu1 = new Menu(shlGms,SWT.POP_UP);
		
		MenuItem menuItem1 = new MenuItem(menu1, SWT.PUSH);
		menuItem1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.exit(0);
			}
		});
		menuItem1.setText("\u9000\u51FA");
		
		trayItem = new TrayItem(tray,SWT.NONE);
		trayItem.setToolTipText("GMS");
		trayItem.setVisible(false);
		trayItem.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/GMS.png"));
		trayItem.addListener(SWT.Show, new Listener(){
			public void handleEvent(Event e){
				//System.out.println("show");
			}
		});
		trayItem.addListener(SWT.Hide, new Listener(){
			public void handleEvent(Event e){
				//System.out.println("hide");
			}
		});
		//托盘单击事件
		trayItem.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event e){
				//System.out.println("selection");
				shlGms.setVisible(true);
				//shlGms.moveAbove(Display.getCurrent().getActiveShell());//要多尝试几种，ON_TOP
				trayItem.setVisible(false);
			}
		});
		//托盘双击事件
		trayItem.addListener(SWT.DefaultSelection, new Listener(){
			public void handleEvent(Event e){
				//System.out.println("default selection");
			}
		});
		trayItem.addListener(SWT.MenuDetect, new Listener(){
			public void handleEvent(Event e){
				//System.out.println("default selection");
				menu1.setVisible(true);
			}
		});*/
		
		//系统托盘***结束***********************************************************************
	}
	
	
	
	@Override
	public void updata() {
		if(RMM.isOpen() == false && open == true){
			refresh();
			//System.out.println("刷新");
			open = false;
		}
	}
	
	class BackgroundThread extends Thread{
		String str = "";
		public BackgroundThread(String str){
			this.str = str;
		}
		public void run(){
			display.asyncExec(new Runnable() {
				
				@Override
				public void run() {
					
					if(btn_creatUp.isVisible() || btn_joinUp.isVisible() || btn_pwdUp.isVisible()){
						table.setBounds(0, 202, 274, 276);
						label.setBounds(10, 179, 254, 20);
						label_2.setBounds(0, 172, 275, 2);
						lbl_remind.setBounds(10, 147, 254, 20);
					}
					else{
						table.setBounds(0, 122, 274, 356);
						label.setBounds(10, 99, 254, 20);
						label_2.setBounds(0, 92, 275, 2);
						lbl_remind.setBounds(10, 67, 254, 20);
					}
					lbl_remind.setText(str);
					lbl_remind.setVisible(true);
					label_2.setVisible(true);
				}
			});
			
			try {
				sleep(2500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			display.asyncExec(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					lbl_remind.setVisible(false);
					label_2.setVisible(false);
					if(btn_creatUp.isVisible() || btn_joinUp.isVisible() || btn_pwdUp.isVisible()){
						label.setBounds(10, 147,254, 20);
						table.setBounds(0, 170, 274, 308);
					}
					else{
						table.setBounds(0, 90, 274, 388);
						label.setBounds(10, 67,254, 20);
					}
													
				}
			});
		}
	}
	
	private void refresh(){
		//刷新列表**********************************************************************************************************
		table.clearAll();
		TableItem[] items = table.getItems();
		int count = 0;
		for(int id:(ArrayList<Integer>)RMM.getRoomByUId(RMM.getUserId())){
			String[] roomInfo = RMC.getRoomInfoByRId(id).split("/");
			if(count < items.length){
				items[count].setText(id + "");
				items[count].setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		    	if(roomInfo[1].equals(user[1])){
		    		if(id == openedRoomID && RMM.isOpen()){
		    			items[count].setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群主群头像_enter_35.png"));
		    		}
		    		else{
		    			items[count].setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群主群头像_35.png"));
		    		}
		    	}
		    	else{
		    		if(id == openedRoomID && RMM.isOpen()){
		    			items[count].setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群头像_enter_35.png"));
		    		}
		    		else{
		    			items[count].setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群头像_35.png"));
		    		}
		    	}				    	
			}
			else{
				TableItem ti =new TableItem(table, SWT.NONE);
		    	ti.setText(id + "");
		    	ti.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		    	if(roomInfo[1].equals(user[1])){
		    		if(id == openedRoomID && RMM.isOpen()){
		    			ti.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群主群头像_enter_35.png"));
		    		}
		    		else{
		    			ti.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群主群头像_35.png"));
		    		}
		    	}
		    	else{
		    		if(id == openedRoomID && RMM.isOpen()){
		    			ti.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群头像_enter_35.png"));
		    		}
		    		else{
		    			ti.setImage(SWTResourceManager.getImage(RoomManageView.class, "/img/群头像_35.png"));
		    		}
		    	}
			}
			count ++;
	    }
		//刷新列表***结束****************************************************************************************************
	
	}


	@Override
	public synchronized void exceptionInfo(Exception Exception) {
		// TODO Auto-generated method stub
		if(!msgOpen){
			msgOpen = true;
			display.asyncExec(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					MessageBox mb = new MessageBox(shlGms, SWT.ERROR);
					mb.setText("网络异常");
					mb.setMessage("网络已断开，请重新登录");
					mb.open();
					System.exit(-1);//网络异常退出
					
				}
			});
		}
	}
}
